package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.cp.CpRankBean;
import com.taptech.doufu.ui.view.RoundImageView;

import java.util.List;

/**
 * cp 列表
 */
public class CpListRecyclerAdapter extends RecyclerView.Adapter<CpListRecyclerAdapter.ViewHolder> {

    List<CpRankBean> dataList;
    private Context context;

    public CpListRecyclerAdapter(Context context, List<CpRankBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cp_list, parent, false);
        return new CpListRecyclerAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        CpRankBean data = dataList.get(position);

        holder.cp_name.setText(data.getName().trim());
        holder.cp_iv.setImageResource(R.drawable.cp_deaf_iv);
        if (data.getIcon() != null && !data.getIcon().equals("")) {
            Picasso.with(context).load(data.getIcon()).into(holder.cp_iv);
        } else {
            holder.cp_iv.setImageResource(R.drawable.cp_deaf_iv);
        }
        holder.flower_num_tv.setText(data.getFlower_count());
        //献花用户
        if (data.getUsers() != null && data.getUsers().size() > 0) {
           // HorizontalListViewAdapter flowerUserAdapter = new HorizontalListViewAdapter(context, HorizontalListViewAdapter.TYPE_CP_LIST, false);
           // flowerUserAdapter.setFlowerInfo(data.getUsers());
//            holder.flowerUserList.setAdapter(flowerUserAdapter);
            try {
                Picasso.with(context).load(data.getUsers().get(0).getUser().getIcon()).into(holder.cp_user_1);
                Picasso.with(context).load(data.getUsers().get(1).getUser().getIcon()).into(holder.cp_user_2);
                Picasso.with(context).load(data.getUsers().get(2).getUser().getIcon()).into(holder.cp_user_3);
                Picasso.with(context).load(data.getUsers().get(3).getUser().getIcon()).into(holder.cp_user_4);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        holder.cp_rank_tv.setText(Integer.toString(position + 1));
        int rank_num = position + 1;
        switch (rank_num) {
            case 1:
                holder.cp_rank_front_iv.setImageResource(R.drawable.cp_rank_first);
                holder.cp_rank_tv.setTextColor(Color.parseColor("#ff626c"));
                holder.cp_rank_front_iv.setVisibility(View.VISIBLE);
                break;
            case 2:
                holder.cp_rank_front_iv.setImageResource(R.drawable.cp_rank_second);
                holder.cp_rank_tv.setTextColor(Color.parseColor("#ff626c"));
                holder.cp_rank_front_iv.setVisibility(View.VISIBLE);
                break;
            case 3:
                holder.cp_rank_front_iv.setImageResource(R.drawable.cp_rank_third);
                holder.cp_rank_tv.setTextColor(Color.parseColor("#ff626c"));
                holder.cp_rank_front_iv.setVisibility(View.VISIBLE);
                break;
            default:
                holder.cp_rank_front_iv.setVisibility(View.GONE);
                holder.cp_rank_tv.setTextColor(Color.parseColor("#8e8e8e"));
                break;
        }
        int trend_num = data.getTrend();
        holder.rank_change_tv.setText(Math.abs(trend_num) + "");
        if (trend_num > 0) {
            holder.cp_rank_iv.setImageResource(R.drawable.cp_rank_up);
            holder.rank_change_tv.setVisibility(View.VISIBLE);
            holder.rank_change_tv.setTextColor(Color.parseColor("#ff626c"));
        } else if (trend_num == 0) {
            holder.cp_rank_iv.setImageResource(R.drawable.cp_rank_equls);
            holder.rank_change_tv.setVisibility(View.INVISIBLE);
        } else {
            holder.cp_rank_iv.setImageResource(R.drawable.cp_rank_down);
            holder.rank_change_tv.setVisibility(View.VISIBLE);
            holder.rank_change_tv.setTextColor(Color.parseColor("#33cc99"));
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView cp_rank_tv;
        public ImageView cp_iv;
        public TextView cp_name;
        public TextView flower_num_tv;

        public ImageView cp_rank_iv;
        public TextView rank_change_tv;
        public ImageView cp_rank_front_iv;

        public RoundImageView cp_user_1;
        public RoundImageView cp_user_2;
        public RoundImageView cp_user_3;
        public RoundImageView cp_user_4;



        public ViewHolder(View itemView) {
            super(itemView);
            cp_name = (TextView) itemView.findViewById(R.id.cp_name);
            cp_iv = (ImageView) itemView.findViewById(R.id.cp_iv);
            cp_rank_iv = (ImageView) itemView.findViewById(R.id.cp_rank_iv);
            cp_rank_front_iv = (ImageView) itemView.findViewById(R.id.cp_rank_front_iv);
            flower_num_tv = (TextView) itemView.findViewById(R.id.flower_num_tv);
            cp_rank_tv = (TextView) itemView.findViewById(R.id.cp_rank_tv);
            rank_change_tv = (TextView) itemView.findViewById(R.id.rank_change_tv);

            cp_user_1 = (RoundImageView) itemView.findViewById(R.id.cp_user_1);
            cp_user_2 = (RoundImageView) itemView.findViewById(R.id.cp_user_2);
            cp_user_3 = (RoundImageView) itemView.findViewById(R.id.cp_user_3);
            cp_user_4 = (RoundImageView) itemView.findViewById(R.id.cp_user_4);

        }
    }
}
